<template>
	<common-card type="form" title="填写课题成果" :loading="loading">
		<el-form ref="form" size="medium" :model="formData" :rules="formRules" :validate-on-rule-change="false">
			<el-form-item label="所属课题" prop="topicId">
				<el-select v-model="formData.topicId" placeholder="选择课题名称" style="width: 100%" disabled>
					<el-option v-for="(item, index) in topicAuditData" :key="index" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item v-if="!!formData.topicId">
				<topic-info :data="checkedData"></topic-info>
			</el-form-item>

			<el-form-item prop="journals" v-if="formData.filed10 === '1'">
				<achievement-form page="edit" type="1" title="期刊论文" v-model="formData.journals"></achievement-form>
			</el-form-item>
			<el-form-item prop="patents" v-if="formData.filed10 === '2'">
				<achievement-form page="edit" type="2" title="授权专利" v-model="formData.patents"></achievement-form>
			</el-form-item>
			<el-form-item prop="meetings" v-if="formData.filed10 === '3'">
				<achievement-form page="edit" type="3" title="会议论文" v-model="formData.meetings"></achievement-form>
			</el-form-item>
			<el-form-item prop="awards" v-if="formData.filed10 === '4'">
				<achievement-form page="edit" type="4" title="获得奖项" v-model="formData.awards"></achievement-form>
			</el-form-item>
		</el-form>
		<template slot="button">
			<template v-if="userRole === 'member'">
				<el-button type="success" icon="sw-icon-btn-save" :loading="saveLoading" @click="handleSaveData()">保存</el-button>
			</template>
			<el-button type="warning" icon="sw-icon-btn-share" :loading="submitLoading" @click="handleSubmitData()">提交</el-button>
		</template>
	</common-card>
</template>

<script>
import topicInfo from '../topicInfo';
import commonCard from '../commonCard';
import achievementForm from './achievement';
import { updateMarvel, marvelDetail } from '@/api/business/tbMarvel';
import { queryUserPageList, queryPageList, queryAuditPageList } from '@/api/business/tbTopic';
export default {
	components: { commonCard, topicInfo, achievementForm },
	props: {
		userRole: { type: String, default: '' },
		dataType: { type: String, default: '' }
	},
	data() {
		return {
			loading: false,
			formData: {},
			formRules: {
				topicId: [{ required: true, message: '请选择课题', trigger: 'change' }]
			},
			topicAuditData: [],
			saveLoading: false,
			submitLoading: false
		};
	},
	computed: {
		dataId() {
			return this.$route.query.id || '';
		},
		topicListRequest() {
			if (this.userRole === 'manager') {
				return queryPageList;
			} else if (this.userRole === 'principal') {
				return queryAuditPageList;
			} else if (this.userRole === 'member') {
				return queryUserPageList;
			}
		},
		checkedData() {
			return this.topicAuditData.find((n) => n.id === this.formData.topicId);
		}
	},
	watch: {
		dataId: {
			async handler(id) {
				this.loading = true;
				await this.getTopicAuditData();
				await this.getDetailData(id);
				this.loading = false;
			},
			immediate: true
		}
	},
	methods: {
		//获取课题数据
		async getTopicAuditData() {
			let params = { page: 1, limit: 9999, audit: '1' };
			let result = await this.topicListRequest(params).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.topicAuditData = result.data || [];
			}
		},

		//获取成果详情
		async getDetailData(id) {
			let result = await marvelDetail(id).then((res) => res.data);
			if (result.code === '0') {
				let datas = this.$detailData(result.data);
				let dataObject = { id: datas.id, topicId: datas.topicId, filed10: datas.filed10 };
				if (datas.filed10 === '1') {
					dataObject['journals'] = this.formatEchoData(datas);
				} else if (datas.filed10 === '2') {
					dataObject['patents'] = this.formatEchoData(datas);
				} else if (datas.filed10 === '3') {
					dataObject['meetings'] = this.formatEchoData(datas);
				} else if (datas.filed10 === '4') {
					dataObject['awards'] = this.formatEchoData(datas);
				}
				this.formData = dataObject;
			}
		},

		//格式化返回数据
		formatEchoData(data) {
			let obj = {};
			let arrs = ['filed1', 'filed2', 'filed3', 'filed4', 'filed5', 'fileJson'];
			arrs.forEach((item) => {
				if (!!data[item]) {
					obj[item] = data[item];
				}
			});
			return [obj];
		},

		//保存数据
		handleSaveData() {
			this.$refs['form'].validateField('topicId', (err) => {
				if (!!err) return false;
				this.saveLoading = true;
				this.$set(this.formData, 'isSave', '2');
				this.submitFormRequest();
			});
		},

		//提交数据
		handleSubmitData() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				this.submitLoading = true;
				this.$set(this.formData, 'isSave', '1');
				this.submitFormRequest();
			});
		},

		//格式化表单参数
		formatFormParams() {
			let arrObject = {};
			let params = this.$lodash.cloneDeep(this.formData);
			params['name'] = this.checkedData.name;
			params['auditId'] = this.checkedData.auditId;
			if (params.filed10 === '1') {
				arrObject = params.journals[0];
				delete params.journals;
			} else if (params.filed10 === '2') {
				arrObject = params.patents[0];
				delete params.patents;
			} else if (params.filed10 === '3') {
				arrObject = params.meetings[0];
				delete params.meetings;
			} else if (params.filed10 === '4') {
				arrObject = params.awards[0];
				delete params.awards;
			}
			return { ...params, ...arrObject };
		},

		//提交表单请求
		async submitFormRequest() {
			let params = this.formatFormParams();
			let result = await updateMarvel(params).then((res) => res.data);
			this.saveLoading = false;
			this.submitLoading = false;
			if (result.code === '0') {
				this.$showCustomMessage('success', '修改成果成功！');
				this.$router.back();
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
